package com.mingqijia.gassafety.authority.vcc.user;

import com.mingqijia.gassafety.authority.api.resp.manager.ManagerRespDTO;
import com.mingqijia.gassafety.authority.api.resp.manager.UserProfileRespDTO;
import com.mingqijia.gassafety.authority.vcc.valueobject.ManagerVO;
import com.mingqijia.gassafety.db.entity.auth.ManagerEntry;
import com.mingqijia.gassafety.shared.utils.MappingTools;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 对象转换接口
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年01月09日 20:37
 */
@Mapper(componentModel = "spring", uses = MappingTools.class)
@ConditionalOnMissingBean(VccUserTranslatorImpl.class)
public interface VccUserTranslator {

    @Mapping(source = "roleId", target = "roleIds")
    ManagerVO toVO(ManagerEntry entry);

    UserProfileRespDTO toProfileRespDTO(ManagerVO vo);

    ManagerRespDTO toRespDTO(ManagerVO vo);

    ManagerRespDTO toRespDTO(ManagerEntry entry);

    default List<ManagerRespDTO> toRespDTO(List<ManagerEntry> entries) {
        return Optional.ofNullable(entries)
                .orElse(Collections.emptyList())
                .stream().map(this::toRespDTO)
                .collect(Collectors.toList());
    }
}
